//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows;
using orientation = System.Windows.Controls.Orientation;

namespace OpenLS.Spreadsheet.UI
{
    ///<summary>
    /// Panel used to arrange groups in a ribbon tab
    ///</summary>
    public class GroupPanel:Panel
    {
        protected override Size ArrangeOverride(Size finalSize)
        {
            //\\  Debug.WriteLine("ArrangeOverride " + GetType());
            double maxValue = 0;
            double containerOffset = ContainerOffset;
            foreach (UIElement child in Children)
            {
                double offset = GetChildOffset(child) - containerOffset;
                if (Orientation == orientation.Vertical)
                {
                    child.Arrange(new Rect(0, offset, finalSize.Width, child.DesiredSize.Height));
                    maxValue = Math.Max(maxValue, offset + child.DesiredSize.Height);
                }
                else
                {
                    child.Arrange(new Rect(offset, 0, child.DesiredSize.Width, finalSize.Height));
                    maxValue = Math.Max(maxValue, offset + child.DesiredSize.Width);
                    
                }
            }
            return base.ArrangeOverride(finalSize);
        }



        public double ContainerOffset
        {
            get { return (double)GetValue(ContainerOffsetProperty); }
            set { SetValue(ContainerOffsetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContainerOffset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContainerOffsetProperty =
            DependencyProperty.Register("ContainerOffset", typeof(double), typeof(GroupPanel), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange));



        public orientation Orientation
        {
            get { return (orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(System.Windows.Controls.Orientation), typeof(GroupPanel), 
            new FrameworkPropertyMetadata(orientation.Horizontal, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsArrange));



        public static double GetChildOffset(DependencyObject obj)
        {
            return (double)obj.GetValue(ChildOffsetProperty);
        }

        public static void SetChildOffset(DependencyObject obj, double value)
        {
            obj.SetValue(ChildOffsetProperty, value);
        }

        // Using a DependencyProperty as the backing store for Offset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ChildOffsetProperty =
            DependencyProperty.RegisterAttached("ChildOffset", typeof(double), typeof(GroupPanel), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsParentMeasure));



        protected override Size MeasureOverride(Size availableSize)
        {
            //\\ Debug.WriteLine("MeasureOverride " + GetType());
            var result = new Size();
            foreach (UIElement child in Children)
            {
                child.Measure(availableSize);
                Size childSize = child.DesiredSize;
                result = Orientation == orientation.Vertical ? new Size(Math.Max(result.Width, childSize.Width), result.Height + childSize.Height) : new Size(result.Width + childSize.Width, Math.Max(result.Height, childSize.Height));
            }
            if (Orientation == orientation.Horizontal)
            {
                if (availableSize.Width < 5000)
                    result = new Size(availableSize.Width, result.Height);
            }
            else
            {
                if (availableSize.Height < 5000)
                    result = new Size(result.Width, availableSize.Height);
                
            }
            return result;
        }
    }
}
